package com.vmj.instructions.comparisions;

import com.vmj.heap.VMObject;
import com.vmj.rtda.Frame;
import com.vmj.rtda.OperandStack;

/**
 * Created by yangshoujun on 2017/8/19 下午11:12.
 */
public class CmpUtils {

    public static void fcmp(Frame frame, boolean flag) {
        OperandStack stack = frame.getOperandStack();
        float v2 = stack.popFloat();
        float v1 = stack.popFloat();
        if(v1 > v2) {
            stack.pushInt(1);
        } else if(v1 == v2) {
            stack.pushInt(0);
        } else if(v1 < v2){
            stack.pushInt(-1);
        } else if(flag) {
            stack.pushInt(1);
        } else {
            stack.pushInt(-1);
        }
    }

    public static boolean acmp(Frame frame) {
        OperandStack stack = frame.getOperandStack();
        VMObject ref2 = stack.popRef();
        VMObject ref1 = stack.popRef();
        return ref1.equals(ref2);
    }
}
